<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属机构" prop="affiliation">
        <el-input
          v-model="queryParams.affiliation"
          placeholder="请输入所属机构"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="queryParams.password"
          placeholder="请输入密码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="盐" prop="salt">
        <el-input
          v-model="queryParams.salt"
          placeholder="请输入盐"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="简称" prop="shortName">
        <el-input
          v-model="queryParams.shortName"
          placeholder="请输入简称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开户行地址" prop="addr">
        <el-input
          v-model="queryParams.addr"
          placeholder="请输入开户行地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开户银行" prop="bank">
        <el-input
          v-model="queryParams.bank"
          placeholder="请输入开户银行"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="银行账号" prop="account">
        <el-input
          v-model="queryParams.account"
          placeholder="请输入银行账号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="税号" prop="taxNo">
        <el-input
          v-model="queryParams.taxNo"
          placeholder="请输入税号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="contact">
        <el-input
          v-model="queryParams.contact"
          placeholder="请输入联系人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="contactTel">
        <el-input
          v-model="queryParams.contactTel"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="统一社会信用代码" prop="usci">
        <el-input
          v-model="queryParams.usci"
          placeholder="请输入统一社会信用代码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司注册地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入公司注册地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业性质" prop="enterpriseNature">
        <el-input
          v-model="queryParams.enterpriseNature"
          placeholder="请输入企业性质"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="成立日期" prop="doe">
        <el-date-picker clearable
          v-model="queryParams.doe"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择成立日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="法人/负责人" prop="corpn">
        <el-input
          v-model="queryParams.corpn"
          placeholder="请输入法人/负责人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="法人/负责人身份证" prop="corpnIdcard">
        <el-input
          v-model="queryParams.corpnIdcard"
          placeholder="请输入法人/负责人身份证"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经营范围" prop="businessScope">
        <el-input
          v-model="queryParams.businessScope"
          placeholder="请输入经营范围"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="营业执照有效期" prop="businessLicenseGp">
        <el-input
          v-model="queryParams.businessLicenseGp"
          placeholder="请输入营业执照有效期"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="注册电话" prop="registrationPhone">
        <el-input
          v-model="queryParams.registrationPhone"
          placeholder="请输入注册电话"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="注册地址" prop="contactAddr">
        <el-input
          v-model="queryParams.contactAddr"
          placeholder="请输入注册地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="注册资本" prop="registeredCapital">
        <el-input
          v-model="queryParams.registeredCapital"
          placeholder="请输入注册资本"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实缴资本" prop="paidinCapital">
        <el-input
          v-model="queryParams.paidinCapital"
          placeholder="请输入实缴资本"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开户许可证/基本户存款信息证" prop="accountLicence">
        <el-input
          v-model="queryParams.accountLicence"
          placeholder="请输入开户许可证/基本户存款信息证"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['scm:enterprise:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['scm:enterprise:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['scm:enterprise:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['scm:enterprise:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="enterpriseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="用户名称" align="center" prop="userName" />
      <el-table-column label="所属机构" align="center" prop="affiliation" />
      <el-table-column label="密码" align="center" prop="password" />
      <el-table-column label="盐" align="center" prop="salt" />
      <el-table-column label="手机号码" align="center" prop="mobile" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="简称" align="center" prop="shortName" />
      <el-table-column label="开户行地址" align="center" prop="addr" />
      <el-table-column label="开户银行" align="center" prop="bank" />
      <el-table-column label="银行账号" align="center" prop="account" />
      <el-table-column label="税号" align="center" prop="taxNo" />
      <el-table-column label="联系人" align="center" prop="contact" />
      <el-table-column label="联系电话" align="center" prop="contactTel" />
      <el-table-column label="统一社会信用代码" align="center" prop="usci" />
      <el-table-column label="公司注册地址" align="center" prop="address" />
      <el-table-column label="企业性质" align="center" prop="enterpriseNature" />
      <el-table-column label="成立日期" align="center" prop="doe" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.doe, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="法人/负责人" align="center" prop="corpn" />
      <el-table-column label="法人/负责人身份证" align="center" prop="corpnIdcard" />
      <el-table-column label="单位联系地址" align="center" prop="contactAddress" />
      <el-table-column label="经营范围" align="center" prop="businessScope" />
      <el-table-column label="公司简介" align="center" prop="profile" />
      <el-table-column label="营业执照扫描件" align="center" prop="businessLicenseImage" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.businessLicenseImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="营业执照有效期" align="center" prop="businessLicenseGp" />
      <el-table-column label="法人/负责人身份证扫描件" align="center" prop="corpnIdcardImage" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.corpnIdcardImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="注册电话" align="center" prop="registrationPhone" />
      <el-table-column label="注册地址" align="center" prop="contactAddr" />
      <el-table-column label="注册资本" align="center" prop="registeredCapital" />
      <el-table-column label="实缴资本" align="center" prop="paidinCapital" />
      <el-table-column label="开户许可证/基本户存款信息证" align="center" prop="accountLicence" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['scm:enterprise:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['scm:enterprise:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改投标企业信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="enterpriseRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户名称" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户名称" />
        </el-form-item>
        <el-form-item label="所属机构" prop="affiliation">
          <el-input v-model="form.affiliation" placeholder="请输入所属机构" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="盐" prop="salt">
          <el-input v-model="form.salt" placeholder="请输入盐" />
        </el-form-item>
        <el-form-item label="手机号码" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="简称" prop="shortName">
          <el-input v-model="form.shortName" placeholder="请输入简称" />
        </el-form-item>
        <el-form-item label="开户行地址" prop="addr">
          <el-input v-model="form.addr" placeholder="请输入开户行地址" />
        </el-form-item>
        <el-form-item label="开户银行" prop="bank">
          <el-input v-model="form.bank" placeholder="请输入开户银行" />
        </el-form-item>
        <el-form-item label="银行账号" prop="account">
          <el-input v-model="form.account" placeholder="请输入银行账号" />
        </el-form-item>
        <el-form-item label="税号" prop="taxNo">
          <el-input v-model="form.taxNo" placeholder="请输入税号" />
        </el-form-item>
        <el-form-item label="联系人" prop="contact">
          <el-input v-model="form.contact" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactTel">
          <el-input v-model="form.contactTel" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="统一社会信用代码" prop="usci">
          <el-input v-model="form.usci" placeholder="请输入统一社会信用代码" />
        </el-form-item>
        <el-form-item label="公司注册地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入公司注册地址" />
        </el-form-item>
        <el-form-item label="企业性质" prop="enterpriseNature">
          <el-input v-model="form.enterpriseNature" placeholder="请输入企业性质" />
        </el-form-item>
        <el-form-item label="成立日期" prop="doe">
          <el-date-picker clearable
            v-model="form.doe"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择成立日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="法人/负责人" prop="corpn">
          <el-input v-model="form.corpn" placeholder="请输入法人/负责人" />
        </el-form-item>
        <el-form-item label="法人/负责人身份证" prop="corpnIdcard">
          <el-input v-model="form.corpnIdcard" placeholder="请输入法人/负责人身份证" />
        </el-form-item>
        <el-form-item label="单位联系地址" prop="contactAddress">
          <el-input v-model="form.contactAddress" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="经营范围" prop="businessScope">
          <el-input v-model="form.businessScope" placeholder="请输入经营范围" />
        </el-form-item>
        <el-form-item label="公司简介" prop="profile">
          <file-upload v-model="form.profile"/>
        </el-form-item>
        <el-form-item label="营业执照扫描件" prop="businessLicenseImage">
          <image-upload v-model="form.businessLicenseImage"/>
        </el-form-item>
        <el-form-item label="营业执照有效期" prop="businessLicenseGp">
          <el-input v-model="form.businessLicenseGp" placeholder="请输入营业执照有效期" />
        </el-form-item>
        <el-form-item label="法人/负责人身份证扫描件" prop="corpnIdcardImage">
          <image-upload v-model="form.corpnIdcardImage"/>
        </el-form-item>
        <el-form-item label="注册电话" prop="registrationPhone">
          <el-input v-model="form.registrationPhone" placeholder="请输入注册电话" />
        </el-form-item>
        <el-form-item label="注册地址" prop="contactAddr">
          <el-input v-model="form.contactAddr" placeholder="请输入注册地址" />
        </el-form-item>
        <el-form-item label="注册资本" prop="registeredCapital">
          <el-input v-model="form.registeredCapital" placeholder="请输入注册资本" />
        </el-form-item>
        <el-form-item label="实缴资本" prop="paidinCapital">
          <el-input v-model="form.paidinCapital" placeholder="请输入实缴资本" />
        </el-form-item>
        <el-form-item label="开户许可证/基本户存款信息证" prop="accountLicence">
          <el-input v-model="form.accountLicence" placeholder="请输入开户许可证/基本户存款信息证" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Enterprise">
import { listEnterprise, getEnterprise, delEnterprise, addEnterprise, updateEnterprise } from "@/api/scm/enterprise";

const { proxy } = getCurrentInstance();

const enterpriseList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userName: null,
    affiliation: null,
    password: null,
    salt: null,
    mobile: null,
    name: null,
    shortName: null,
    addr: null,
    bank: null,
    account: null,
    taxNo: null,
    contact: null,
    contactTel: null,
    usci: null,
    address: null,
    enterpriseNature: null,
    doe: null,
    corpn: null,
    corpnIdcard: null,
    contactAddress: null,
    businessScope: null,
    profile: null,
    businessLicenseImage: null,
    businessLicenseGp: null,
    corpnIdcardImage: null,
    registrationPhone: null,
    contactAddr: null,
    registeredCapital: null,
    paidinCapital: null,
    accountLicence: null,
  },
  rules: {
    userName: [
      { required: true, message: "用户名称不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询投标企业信息列表 */
function getList() {
  loading.value = true;
  listEnterprise(queryParams.value).then(response => {
    enterpriseList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    userName: null,
    affiliation: null,
    password: null,
    salt: null,
    mobile: null,
    name: null,
    shortName: null,
    addr: null,
    bank: null,
    account: null,
    taxNo: null,
    contact: null,
    contactTel: null,
    usci: null,
    address: null,
    enterpriseNature: null,
    doe: null,
    corpn: null,
    corpnIdcard: null,
    contactAddress: null,
    businessScope: null,
    profile: null,
    businessLicenseImage: null,
    businessLicenseGp: null,
    corpnIdcardImage: null,
    registrationPhone: null,
    contactAddr: null,
    registeredCapital: null,
    paidinCapital: null,
    accountLicence: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  proxy.resetForm("enterpriseRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加投标企业信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getEnterprise(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改投标企业信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["enterpriseRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateEnterprise(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addEnterprise(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除投标企业信息编号为"' + _ids + '"的数据项？').then(function() {
    return delEnterprise(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('scm/enterprise/export', {
    ...queryParams.value
  }, `enterprise_${new Date().getTime()}.xlsx`)
}

getList();
</script>
